#!/bin/sh

grep "Linux" /proc/version
if [ $? -eq 0 ]; then
############################################
### 		Working with Linux
HOMEDIR=/group/project/statmt2/chongde
DECODER=$HOMEDIR/tools/bin/moses.irst.1892
SRILM=$HOMEDIR/tools/srilm/bin/i686
IRSTLM=$HOMEDIR/tools/irstlm/bin/i686
############################################
else
grep "CYGWIN" /proc/version
	if [ $? -eq 0 ]; then
############################################
### 		Working with Cygwin
HOMEDIR=/smt
DECODER=$HOMEDIR/moses/moses-cmd/src/moses.exe
SRILM=$HOMEDIR/tools/srilm/bin/cygwin
IRSTLM=$HOMEDIR/tools/irstlm/bin/i686-pc-cygwin
############################################
	else
		echo "The system is neither Linux nor Cygwin, exit!"
		exit 1;
	fi
fi

# Global
BASEDIR=$HOMEDIR/lattice_decoding
MOSESSCRIPTS=$HOMEDIR/tools/moses-scripts
MOSES=$HOMEDIR/tools/moses

# Command line argument $1
MINI=mini
NOR=normal

if [ "$1" = "$NOR" ]; then
	WORKDIR=$HOMEDIR/tmp/normal_ltc
	DATADIR=$BASEDIR/normal
	TEST_DATA=$DATADIR/eval06.v1.4.chi
elif [ "$1" = "$MINI" ]; then
	WORKDIR=$HOMEDIR/tmp/mini_ltc
	DATADIR=$BASEDIR/mini
	TEST_DATA=$DATADIR/nor_test.chi
	#TEST_DATA=$DATADIR/ltc_test.chi
else
	echo "Try: $0 mini/normal";
	exit 1;
fi

TR_CLEAN_CHI=$WORKDIR/corpus/training.clean.chi
TR_CLEAN_ENG=$WORKDIR/corpus/training.clean.eng
TR_LOW_CHI=$WORKDIR/corpus/training.low.chi
TR_LOW_ENG=$WORKDIR/corpus/training.low.eng
TR_LOW_STEM=$WORKDIR/corpus/training.low
TR_LM=$HOMEDIR/tools/lm/training.blm
TR_PT=$WORKDIR/model/phrase-table.0-0.gz
TU_CHI=$DATADIR/tuning.chi
TU_ENG=$DATADIR/tuning.eng
#MOSES_INI=$DATADIR/moses.ini
MOSES_INI_MODEL=$WORKDIR/model/moses.ini
MOSES_INI_REUSED=$WORKDIR/tuning/moses.weight-reused.ini
MOSES_INI_TEST=$WORKDIR/eval/filtered/moses.ini
TE_FILTERED=$WORKDIR/eval/filtered
TE_OUT=$WORKDIR/eval/output
TE_OUT_DETOK=$WORKDIR/eval/output.detok
TE_OUT_SGM=$WORKDIR/eval/output.sgm

STEP=1
echo "----------------------------------------------------------------"
echo "Step $STEP : make dirs"
cd $BASEDIR
mkdir -p $WORKDIR/corpus
mkdir -p $WORKDIR/model
mkdir -p $WORKDIR/tuning
mkdir -p $WORKDIR/eval

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP: clean corpus"
if [ ! -f $WORKDIR/corpus/training.clean.chi ]; then
	$MOSESSCRIPTS/training/clean-corpus-n.perl $DATADIR/training chi eng $WORKDIR/corpus/training.clean 1 80
else
	echo "Skipped"
fi

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : lowercase corpus"
if [ ! -f $TR_LOW_CHI ]; then
  $BASEDIR/scripts/lowercase.perl < $TR_CLEAN_CHI > $TR_LOW_CHI
  $BASEDIR/scripts/lowercase.perl < $TR_CLEAN_ENG > $TR_LOW_ENG
else
	echo "Skipped"
fi

#STEP=`expr $STEP + 1`
#echo "----------------------------------------------------------------"
#echo "Step $STEP : create lm"
#if [ ! -f $TR_LM ]; then
#  echo "$SRILM/ngram-count -order 5 -interpolate -kndiscount -text $TR_LOW_ENG -lm $TR_LM"
#  $SRILM/ngram-count -order 5 -interpolate -kndiscount -text $TR_LOW_ENG -lm $TR_LM
#  $IRSTLM/compile-lm $TR_LM $TR_LM_BIN
#else
#	echo "Skipped"
#fi

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : training with giza++"
if [ ! -f $TR_PT ]; then
	$MOSESSCRIPTS/training/train-factored-phrase-model.perl -scripts-root-dir $MOSESSCRIPTS -root-dir $WORKDIR -corpus $TR_LOW_STEM -f chi -e eng -alignment grow-diag-final-and -reordering msd-bidirectional-fe -lm 0:5:$TR_LM:1
else
	echo "Skipped"
fi

#######################################################################
##		tuning step
if [ $1 = $NOR ]; then	# Normal corpus
	if [ ! -f $MOSES_INI ]; then # if moses.ini not exist, then tuning

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : tuning"
if [ ! -f $WORKDIR/tuning/moses.ini ]; then
echo
$MOSESSCRIPTS/training/mert-moses.pl $TU_CHI $TU_ENG $DECODER $MOSES_INI_MODEL --working-dir $WORKDIR/tuning --rootdir $MOSESSCRIPTS
else
	echo "Skipped"
fi # this step

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : reuse moses.ini"
if [ ! -f $MOSES_INI_REUSED ]; then
$BASEDIR/reuse-weights.perl $WORKDIR/tuning/moses.ini < $MOSES_INI_MODEL > $MOSES_INI_REUSED 
else
	echo "Skipped"
fi # this step
	else # if moses.ini exist, copy to working dir
		cp $MOSES_INI $MOSES_INI_REUSED
	fi # deal with moses.ini		
fi # if NORMAL

if [ "$1" = "$MINI" ]; then
	cp $MOSES_INI_MODEL $MOSES_INI_REUSED
fi

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : filter test set"
## need modification for lattice input
if [ ! -d $TE_FILTERED ]; then
$MOSESSCRIPTS/training/filter-and-binarize-model-given-input.pl $MOSES/misc/processPhraseTable $TE_FILTERED $MOSES_INI_REUSED $TEST_DATA
else
	echo "Skipped"
fi

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : decode test set"
if [ $1 = $NOR ]; then
if [ ! -f $TE_OUT ]; then
$DECODER  -drop-unknown -config $MOSES_INI_TEST -input-file $TEST_DATA > $TE_OUT
else
	echo "Skipped"
fi
elif [ $1 = $MINI ];then
$DECODER  -drop-unknown  -config $MOSES_INI_TEST -input-file $TEST_DATA > $TE_OUT
#$DECODER  -drop-unknown -inputtype 2 -config $MOSES_INI_TEST -input-file $TEST_DATA > $TE_OUT
fi

if [ $? -eq 0 ]; then
	exit 1;
fi 

if [ $1 = $NOR ]; then	# Normal corpus, evaluation
STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : detokenize output"
if [ ! -f $TE_OUT_DETOK ]; then
$BASEDIR/scripts/detokenizer.perl -l en < $TE_OUT > $TE_OUT_DETOK

if [ $? -eq 0 ]; then
	exit 1;
fi 

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : wrap output in SGML"
if [ ! -f $TE_OUT_SGM ]; then
$BASEDIR/scripts/wrap-xml.perl $DATADIR/eval06-nist-ref.sgm en < $TE_OUT_DETOK > $TE_OUT_SGM

if [ $? -eq 0 ]; then
	exit 1;
fi 

STEP=`expr $STEP + 1`
echo "----------------------------------------------------------------"
echo "Step $STEP : BLEU"
if [ -f $TE_OUT_SGM ]; then
$BASEDIR/mteval-v11b.pl -r $DATADIR/eval06-nist-ref.sgm -t $TE_OUT_SGM -s $DATADIR/eval06-nist-src.sgm -c 
fi # if NORMAL eval


